/**
 * 获取过期项目
 * @周涛2020-05-25
 */
'use strict';
const db = uniCloud.database();
const dbCmd = db.command
exports.main = async (event, context) => {
	let {
		relevanceUserID
	} = event;
	let data = []
	let historyProjectResult = await db.collection('projects').where({ //自己创建的
		relevanceUserID,
		vaildDate: dbCmd.lt(new Date().getTime())
	}).get()

	let joinHistoryProjectResult = await db.collection("projects").where({ //获取加入的
		project: {
			applyPerson: dbCmd.elemMatch({
				opinion: 1,
				userID: relevanceUserID
			})
		},
		vaildDate: dbCmd.lt(new Date().getTime())
	}).get()

	if (historyProjectResult.affectedDocs !== 0 || historyProjectResult.data.length !== 0) {
		data.push(...historyProjectResult.data)
	}
	if (joinHistoryProjectResult.affectedDocs !== 0 || joinHistoryProjectResult.data.length !== 0) {
		data.push(...joinHistoryProjectResult.data)
	}


	if (data.length > 0) {
		return {
			status: 0,
			data: historyProjectResult.data
		}
	} else {
		return {
			status: 1,
			msg: "没有历史项目"
		}
	}


	return {
		status: -1,
		msg: "稍后重试"
	}
};
